<template>
	<view>
		<view id="_drag_button" class="drag" :style="'left: ' + left + 'px; top:' + top + 'px;'"
			@touchstart="touchstart" @touchmove.stop.prevent="touchmove" @touchend="touchend"
			@tap.stop.prevent="click" :class="{transition: isDock && !isMove }">
			<image src="@/static/wine-index/detail-cart.png" mode=""></image>
			<text v-if="carNum">{{carNum}}</text>
		</view>
	</view>
</template>

<script>
	const http = require('../../utils/http.js')
	export default {
		name: 'drag-button',
		props: {
			isDock: {
				type: Boolean,
				default: false
			},
			existTabBar: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				top: 0,
				left: 0,
				width: 0,
				height: 0,
				offsetWidth: 0,
				offsetHeight: 0,
				windowWidth: 0,
				windowHeight: 0,
				isMove: true,
				edge: 10,
				carNum:null
			}
		},
		
		mounted() {
			
			const sys = uni.getSystemInfoSync();

			this.windowWidth = sys.windowWidth;
			this.windowHeight = sys.windowHeight;

			// #ifdef APP-PLUS
			this.existTabBar && (this.windowHeight -= 50);
			// #endif
			if (sys.windowTop) {
				this.windowHeight += sys.windowTop;
			}
			console.log(sys)

			const query = uni.createSelectorQuery().in(this);
			query.select('#_drag_button').boundingClientRect(data => {
				this.width = data.width;
				this.height = data.height;
				this.offsetWidth = data.width / 2;
				this.offsetHeight = data.height / 2;
				this.left = this.windowWidth - this.width - this.edge;
				this.top = this.windowHeight - this.height*3 - this.edge ;
			}).exec();
			
		},
		
		methods: {
			getCarnum(){
						//获取购物车数字接口
						const params = {
						  url: '/gscloud_product/a/shop_cart/prod_count',
						  method: 'GET',
						  data: {},
						  callBack: res => {
								console.log('车车数字')
								console.log(res)
								this.carNum = res
								console.log('车车数字')
						  }
						}
						if (uni.getStorageSync('token')) {
						  http.request(params)
						}
				},
			click() {
				// 跳转购物车页面
				uni.navigateTo({
					url: '/pages/cart/cart'
				})
				console.log('ppppppppp')
				this.$emit('btnClick');
			},
			touchstart(e) {
				this.$emit('btnTouchstart');
			},
			touchmove(e) {
				// 单指触摸
				if (e.touches.length !== 1) {
					return false;
				}

				this.isMove = true;

				this.left = e.touches[0].clientX - this.offsetWidth;

				let clientY = e.touches[0].clientY - this.offsetHeight;
				// #ifdef H5
				clientY += this.height;
				// #endif
				let edgeBottom = this.windowHeight - this.height - this.edge;

				// 上下触及边界
				if (clientY < this.edge) {
					this.top = this.edge;
				} else if (clientY > edgeBottom) {
					this.top = edgeBottom;
				} else {
					this.top = clientY
				}

			},
			touchend(e) {
				if (this.isDock) {
					let edgeRigth = this.windowWidth - this.width - this.edge;

					if (this.left < this.windowWidth / 2 - this.offsetWidth) {
						this.left = this.edge;
					} else {
						this.left = edgeRigth;
					}

				}

				this.isMove = false;

				this.$emit('btnTouchend');
			},
		}
	}
</script>

<style lang="scss" scoped>
	.drag {
		position: relative;
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: rgba(0, 0, 0, 0.5);
		color: white;
		background: #23ff61;
		box-shadow: 0px 4rpx 20rpx 6rpx rgba(237, 237, 237, 0.75);
		border: 2rpx solid #EAEAEA;
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
		position: fixed;
		z-index: 999999;

		&.transition {
			transition: left .3s ease, top .3s ease;
		}

		image {
			width: 50rpx;
			height: 50rpx;
			z-index: 1000000;
		}

		text {
			position: absolute;
			right: 1%;
			top: 2%;
			z-index: 1000001;
			width: 30rpx;
			height: 30rpx;
			background: #FFFFFF;
			border: 2rpx solid #C30D23;
			color: #C30D23;
			line-height: 32rpx;
			text-align: center;
			border-radius: 50%;
			font-size: 22rpx;
			font-weight: 400;
		}
	}
</style>
